# encoding: utf-8
# @File  : config.py
# @Author: shaoyun
# @Date  :  2025/05/11

import os
from typing import Optional, List
from pydantic_settings import BaseSettings


class Settings(BaseSettings):
    # API配置
    API_V1_STR: str = "/api/v1"
    PROJECT_NAME: str = "分布式爬虫管理系统"
    VERSION: str = "0.1.0"

    # 安全配置
    SECRET_KEY: str = os.getenv("SECRET_KEY", "your-secret-key-here")
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 15  # 15分钟

    # ✨ 新增：Refresh Token 配置
    REFRESH_TOKEN_EXPIRE_DAYS: int = 7  # Refresh token 7天过期
    REFRESH_TOKEN_ROTATION_ENABLED: bool = True  # 启用 token rotation
    # 数据库配置
    MONGODB_URL: str = os.getenv("MONGODB_URL", "mongodb://localhost:27017")
    MONGODB_DB: str = os.getenv("MONGODB_DB", "crawler_manager")

    MYSQL_USER: str = os.getenv("MYSQL_USER", "root")
    MYSQL_PASSWORD: str = os.getenv("MYSQL_PASSWORD", "root")
    MYSQL_HOST: str = os.getenv("MYSQL_HOST", "localhost")
    MYSQL_PORT: str = os.getenv("MYSQL_PORT", "3306")
    MYSQL_DB: str = os.getenv("MYSQL_DB", "crawler_manager")

    REDIS_HOST: str = os.getenv("REDIS_HOST", "localhost")
    REDIS_PORT: int = int(os.getenv("REDIS_PORT", "6379"))
    REDIS_DB: int = int(os.getenv("REDIS_DB", "0"))
    REDIS_PASSWORD: Optional[str] = os.getenv("REDIS_PASSWORD")

    # DeepSeek API配置
    DEEPSEEK_API_KEY: Optional[str] = os.getenv("DEEPSEEK_API_KEY")
    DEEPSEEK_BASE_URL: str = os.getenv("DEEPSEEK_BASE_URL", "https://api.deepseek.com")
    DEEPSEEK_MODEL: str = os.getenv("DEEPSEEK_MODEL", "deepseek-reasoner")  # 可选: deepseek-chat 或 deepseek-reasoner

    # Docker配置
    DOCKER_ENABLED: bool = os.getenv("DOCKER_ENABLED", "true").lower() == "true"
    DOCKER_NETWORK: str = os.getenv("DOCKER_NETWORK", "crawler_network")

    # 节点配置
    NODE_HEARTBEAT_INTERVAL: int = 30  # 秒
    NODE_HEARTBEAT_TIMEOUT: int = 120  # 秒
    HEALTH_CHECK_INTERVAL: int = 60  # 健康检查间隔（秒）

    # 节点性能阈值配置
    CPU_THRESHOLD: float = 85.0  # CPU使用率告警阈值
    MEMORY_THRESHOLD: float = 85.0  # 内存使用率告警阈值

    # 任务配置
    MAX_RETRY_COUNT: int = 3  # 最大重试次数
    TASK_TIMEOUT: int = 3600  # 任务超时时间（秒）
    TASK_BATCH_SIZE: int = 10  # 批量处理任务数量

    # 性能监控配置
    PROMETHEUS_PORT: int = int(os.getenv("PROMETHEUS_PORT", "9090"))
    PUSHGATEWAY_PORT: int = int(os.getenv("PUSHGATEWAY_PORT", "9091"))
    GRAFANA_HOST: str = os.getenv("GRAFANA_HOST", "localhost")
    GRAFANA_PORT: int = int(os.getenv("GRAFANA_PORT", "3000"))
    GRAFANA_API_KEY: Optional[str] = os.getenv("GRAFANA_API_KEY")

    # 任务队列配置
    TASK_QUEUE_KEY: str = "task_queue"
    PRIORITY_QUEUE_KEY: str = "priority_task_queue"

    # 节点相关Redis键前缀
    NODE_KEY_PREFIX: str = "node:"
    HEARTBEAT_KEY_PREFIX: str = "heartbeat:"
    NODE_STATUS_KEY_PREFIX: str = "node_status:"

    # 定时任务配置
    CRON_CHECK_INTERVAL: int = 60  # 定时任务检查间隔（秒）
    ENABLE_CRON_PROCESSOR: bool = os.getenv("ENABLE_CRON_PROCESSOR", "true").lower() == "true"

    # 其他配置
    LOG_LEVEL: str = os.getenv("LOG_LEVEL", "INFO")
    LOG_FILE_PATH: str = os.getenv("LOG_FILE_PATH", "logs/app_{time:YYYY-MM-DD}.log")
    ALLOWED_HOSTS: List[str] = ["*"]

    @property
    def SQLALCHEMY_DATABASE_URI(self) -> str:
        """返回MySQL连接URI"""
        return f"mysql+pymysql://{self.MYSQL_USER}:{self.MYSQL_PASSWORD}@{self.MYSQL_HOST}:{self.MYSQL_PORT}/{self.MYSQL_DB}?charset=utf8mb4"

    class Config:
        case_sensitive = True
        env_file = ".env"


settings = Settings()